<template>
  <view>
    <view class="pd24" :class="direction == 'row' ? 'title-header flex' : 'column'">
      <view class="title flex" :class="{ 'inventory-view': name !== '总览' }">{{ name }}</view>
      <view class="flex">
        <ve-picker-inline v-if="showTimeselect" :slotStatus="true" v-model="time.id" :range="timeMap" @change="changeTime">
          <template #picker-line>
            <view class="data-select">
              <ve-icon name="filtergray" size="24" />
              <text class="filter">{{ time.name }}</text>
            </view>
          </template>
        </ve-picker-inline>
        <ve-picker-inline
          class="ml24"
          v-if="showChannel"
          :slotStatus="true"
          v-model="channel.value"
          :range="channelList"
          @change="changeChannel"
        >
          <template #picker-line>
            <view class="data-select">
              <ve-icon name="filtergray" size="24" />
              <text class="filter">{{ channel.label }}</text>
            </view>
          </template>
        </ve-picker-inline>
        <remote-select
          class="ml24"
          :slotStatus="true"
          :type="remoteSelectSitList.attrs.type"
          v-model="sitId"
          v-model:label="remoteSelectSitList.label"
          @change="changesitId"
          :outData="siteList"
          required
        >
          <template #remote-select>
            <view class="data-select">
              <ve-icon name="filtergray" size="24" />
              <text class="filter">{{ remoteSelectSitList.label }}</text>
            </view>
          </template>
        </remote-select>
      </view>
    </view>
    <view v-if="slotStatus" class="content-view">
      <slot name="tips" />
    </view>
  </view>
</template>

<script setup>
  import { ref, watch, onMounted } from 'vue'
  import { onLoad } from '@dcloudio/uni-app'

  const props = defineProps({
    name: {
      type: String,
      default: '',
    },
    direction: {
      type: String,
      default: 'row',
    },
    slotStatus: {
      type: Boolean,
      default: false,
    },
    siteList: {
      type: Array,
      default: () => [],
    },
    showTimeselect: {
      type: Boolean,
      default: false,
    },
    showChannel: {
      type: Boolean,
      default: false,
    },
    timeMap: {
      type: Array,
    },
    channelList: {
      type: Array,
      default: () => [
        {
          label: '全部渠道',
          value: '',
        },
        {
          label: '飞猪',
          value: -1,
        },
        {
          label: '自营',
          value: 0,
        },
        {
          label: '携程',
          value: 1,
        },
        {
          label: '悟空',
          value: 2,
        },
        {
          label: '哈啰',
          value: 3,
        },
        {
          label: '铁行',
          value: -3,
        },
        {
          label: '滴滴',
          value: -2,
        },
        {
          label: '飞租',
          value: -4,
        },
        {
          label: '其他',
          value: 99,
        },
      ],
    },
    channelValue: {
      type: String,
      default: '',
    },
    timeValue: {
      type: Number,
      default: 1,
    },
  })
  const emits = defineEmits(['onchangeSite', 'onchangeTime'])

  const sitId = ref('全部门店')
  let remoteSelectSitList = {
    label: '全部机构',
    type: 'remote-select',
    attrs: {
      title: '选择机构',
      type: 'org_selectAuthListByOrgNameLike',
    },
  }

  const channel = ref({
    value: '',
    label: '全部渠道',
  })
  onMounted(() => {
    if (props.name == '总览') {
      remoteSelectSitList = {
        label: '全部机构',
        type: 'remote-select',
        attrs: {
          type: 'org_selectAuthListByOrgNameLike',
        },
      }
    } else {
      remoteSelectSitList = {
        label: '全部门店',
        type: 'remote-select',
        attrs: {
          type: 'sitedataboard',
          params: {
            oldId: sitId.value,
          },
        },
      }
    }
  })
  onLoad((options) => {
    sitId.value = options.siteId ? options.siteId : '全部门店'
    if (options.siteId) {
      remoteSelectSitList.label = options.siteId
    }
  })

  let time = ref({})
  if (props.timeMap) {
    time = ref({
      id: props.timeValue,
      name: props.timeMap[props.timeValue].label,
    })
    if (props.name === '总览') {
      time.value.name = '本月'
      sitId.value = '全部机构'
    }
  }

  const changesitId = (value, option) => {
    emits('onchangeSite', value, option)
  }
  const changeChannel = (value, option) => {
    emits('onchangeChannel', value, option)
  }
  const changeTime = (value, option) => {
    emits('onchangeTime', value, option)
    time.value.name = option.label
  }

  watch(
    () => props.channelValue,
    (val) => {
      if (val !== undefined) {
        for (let i = 0; i < props.channelList.length; i++) {
          if (props.channelList[i].value === val) {
            channel.value = { ...props.channelList[i] }
          }
        }
      }
    },
  )
</script>

<style lang="scss" scoped>
  .flex {
    display: flex;
    align-items: center;
  }
  .pd24 {
    padding: 24rpx;
  }
  .inventory-view::before {
    content: '';
    display: inline-block;
    width: 6rpx;
    height: 32rpx;
    background: #3072f6;
    margin-right: 16rpx;
  }
  .title-header {
    justify-content: space-between;
    width: 100%;
    .title {
      font-size: 32rpx;
      font-weight: 600;
      color: #191c27;
      word-break: keep-all;
    }
  }
  .ml24 {
    margin-left: 24rpx;
  }
  .column {
    height: 90px;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
  }
  .content-view {
    background: #fff;
    // margin-top: 16rpx;
    padding: 32rpx 24rpx;
  }

  .filter {
    display: inline-block;
    vertical-align: middle;
    margin-left: 8rpx;
    max-width: 300rpx;
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
  }
  //日期
  .data-select {
    position: relative;
    display: flex;
    align-items: center;
    height: 48rpx;
    line-height: 48rpx;
    font-size: 24rpx;
    font-weight: 400;
    padding: 0 16rpx;
    border: 1px solid #dadbdd;
  }
</style>
